package php4dvdPages;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.ui.Select;
import php4dvdTests.MyPageFactory;
import php4dvdTests.Roles;
import php4dvdTests.User;

import java.util.ArrayList;
import java.util.List;

import static org.openqa.selenium.support.ui.ExpectedConditions.alertIsPresent;

/**
 * @author Anna Alexeyenko
 *         Date: 1/26/13
 */
public class UserManagementPage extends InternalPage {

    private static final String ADMIN = "admin";
    private List<User> cashedUsers;

    @FindBy(id = "username")
    private WebElement userNameField;

    @FindBy(name = "email")
    private WebElement emailField;

    @FindBy(id = "password")
    private WebElement passwordField;

    @FindBy(id = "password2")
    private WebElement password2Field;

    @FindBy(name = "permission")
    private WebElement role;

    @FindBy(name = "submit")
    private WebElement saveButton;

    @FindBy(xpath = "//*[@class='content']/table/tbody/tr")
    private List<WebElement> allUsers;

    public UserManagementPage addNewUser(User user) {
        userNameField.sendKeys(user.getName());
        emailField.sendKeys(user.getEmail());
        passwordField.sendKeys(user.getPassword());
        password2Field.sendKeys(user.getPassword());
        Select roleField = new Select(role);
        roleField.selectByVisibleText(user.getRole());
        saveButton.click();
        cashedUsers = null;
        return MyPageFactory.getPage(driver, UserManagementPage.class);
    }

    public UserManagementPage deleteUser(User userToDelete) {
        for (WebElement user : allUsers) {
            if (userIsInTheUserList(userToDelete)) {
                user.findElement(By.xpath("//td[6]/a")).click();
                waitForElement().until(alertIsPresent()).accept();
                break;
            }
        }
        cashedUsers = null;
        return MyPageFactory.getPage(driver, UserManagementPage.class);
    }


    public UserManagementPage deleteAllUsersExceptAdmin() {
        for (User user : getUsers()) {
            if (!user.getName().equals(ADMIN)) {
                deleteUser(user);
            }
        }
        cashedUsers = null;
        return MyPageFactory.getPage(driver, UserManagementPage.class);

    }

    public List<User> getUsers() {
        if (cashedUsers == null) {
            cashedUsers = new ArrayList<User>();
            for (WebElement user : allUsers) {
                cashedUsers.add(convertToUserObject(user));
            }
        }
        return new ArrayList<User>(cashedUsers);
    }

    public int getUserCount() {
        return allUsers.size();
    }

    public boolean userIsInTheUserList(User userToCheck) {
        return getUsers().contains(userToCheck);
    }

    private User convertToUserObject(WebElement user) {
        List<WebElement> userFields = user.findElements(By.tagName("td"));
        return new User().
                setName(userFields.get(1).getText()).
                setEmail(userFields.get(2).getText()).
                setRole(Roles.valueOf(userFields.get(3).getText().toUpperCase()));
    }


}
